package jp.co.monex.business
{
	import flash.events.EventDispatcher;
	
	import jp.co.monex.debug_version.DebugInstance;
	
	import mx.messaging.Channel;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.messaging.channels.SecureAMFChannel;
	import mx.messaging.events.ChannelEvent;
	import mx.messaging.events.ChannelFaultEvent;
	import mx.resources.ResourceBundle;
	
	public class channelInstace extends EventDispatcher
	{
		[ResourceBundle("version")]
		public static var versionRBS:ResourceBundle;
		
		
		
		[Bindable]
		public var defaultDate:Date;
	
		public var custChannel:Channel = new AMFChannel();
		public var custChannel_sc:Channel =  new SecureAMFChannel();
		
		public var cutChannelSet:ChannelSet = new ChannelSet();
		
		public static const _instance:channelInstace = new channelInstace();
		
		public function channelInstace()
		{
			if ( _instance != null ) {
				
				throw new Error( "Please use the instance property to access." );
				 }
		}
		
		public static function get instance():channelInstace {
			return _instance;
		}
		
		public function createChannel():ChannelSet{
			//mx.controls.Alert.show("DomainName"+Application.application.domainStatus);
			custChannel_sc = new SecureAMFChannel("my-secure-amf",versionRBS.getString('amfScurls'));
			custChannel_sc.connectTimeout=2000;
			custChannel = new AMFChannel("my-amf",versionRBS.getString('amfurls'));
			  
			cutChannelSet.addChannel(custChannel);
			cutChannelSet.addChannel(custChannel_sc);
			DebugInstance.instance.debugLine(custChannel.url,"Channel URL 1");
			DebugInstance.instance.debugLine(custChannel_sc.url,"Channel URL 2S");
			//cutChannelSet.clustered = true;
			
			cutChannelSet.addEventListener(ChannelFaultEvent.FAULT,testFunFault);
			cutChannelSet.addEventListener(ChannelEvent.CONNECT,testFunConnect)
			return cutChannelSet;  
		}
		
		public function testFunFault(ev:ChannelFaultEvent):void{
			DebugInstance.instance.debugLine(ev.faultDetail.toString(),"Channel Fail: ");	
			DebugInstance.instance.debugLine(ev.faultCode.toString(),"Channel Fail: ");
			DebugInstance.instance.debugLine(ev.faultString,"Channel Fail: ");
			
		}
		
		public function testFunConnect(ev:ChannelEvent):void{
			DebugInstance.instance.debugLine(ev.connected.toString(),"Channel Connected");
		}
	}
}
